﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml;
using System.IO;
using System.Collections;
using Microsoft.Win32;
using System.Xml.XPath;
using System.Data;


namespace CooStationsToolsNew
{
    /// <summary>
    /// InitWindow.xaml 的交互逻辑
    /// </summary>
    public partial class InitWindow : Window
    {
       
     
        public static bool isnewApp = false;
        

        ProjectTools pt = new ProjectTools();

       // Canvas thiscan;
        public string LastText = ""; //上次打开路径
        
          public InitWindow()
        {
            InitializeComponent();
         //   thiscan = canvas;
            GetPastUser();
        }
 
          //public void InitCanvas()
          //{
          //    GetPastUser();
             
          //}

        private void GetPastUser()
        {
            XmlDocument xmlConfig = new XmlDocument();

            xmlConfig.Load(App.appPath + "config.xml");

            XmlNodeList list = xmlConfig.ChildNodes[1].ChildNodes;

            XmlNodeList pastList = pt.GetList("pastuse",list);

            for (int i = 0; i < pastList.Count; i++)
            {
                XmlElement xe = (XmlElement)pastList[i];
                if(i<5)
                {
                    GreatPast(xe.GetAttribute("proName"), xe.GetAttribute("proPath"),i+1);
                }
              
            }
        }

        private void GreatPast(string proName,string proPath,int index)
        {
            try
            {
                newProUC newpro = new newProUC(proName);
                newpro.Tag = proPath;
                newpro.Name = "ProName";
                newpro.MouseLeftButtonDown += new MouseButtonEventHandler(newpro_MouseLeftButtonDown);
                newpro.Width = 163.0;
                newpro.Height = 27.0;
                grid1.Children.Add(newpro);
                Grid.SetRow(newpro, index - 1);
            }
            catch { }
        }

        void newpro_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
           // App.dt = pt.OpenProject(((newProUC)sender).Tag.ToString());
            ProgressWinOpen pwo = new ProgressWinOpen(((newProUC)sender).Tag.ToString());
            pwo.Owner = this;
            pwo.ShowDialog();

            if (App.dt != null)
            {
                isnewApp = true;
                this.DialogResult = true;
                this.Close();
            }
            else
            {
                RemovePaset(((newProUC)sender).Name);
            }
        }

        private void RemovePaset(string  sender)
        {
            foreach (UIElement i in grid1.Children)
            {
                if (i is newProUC)
                {
                    newProUC t = (newProUC)i;
                    if (t.Name == sender)
                    {
                        grid1.Children.Remove(t);
                        break;
                    }
                }
            }
        }

        private void OpenProjectBt_Click(object sender, RoutedEventArgs e)
        {
            OpenFileDialog openFiledDialog = new OpenFileDialog();
            if (LastText == null || LastText == "")
            {
                openFiledDialog.InitialDirectory = System.Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
            }
            else
            {
                openFiledDialog.InitialDirectory = LastText;
            }
            openFiledDialog.Filter = "文本文件(*.cfy)|*.cfy|所有文件(*.*)|*.*";
            openFiledDialog.FilterIndex = 1;

            Nullable<bool> result = openFiledDialog.ShowDialog();

            if (result == true)
            {
                pt.ClearTemp();
                LastText = openFiledDialog.FileName;
               // App.dt = pt.OpenProject(openFiledDialog.FileName);
                ProgressWinOpen pwo = new ProgressWinOpen(openFiledDialog.FileName);
                pwo.Owner = this;
                pwo.ShowDialog();
                if (App.dt != null)
                {
                   // pt.InitTheWork(App.dt, thiscan);
                    isnewApp = true;
                    this.DialogResult = true;
                    this.Close();
                }
            }
        }

        private void NewWeb_Click(object sender, RoutedEventArgs e)
        {
            App.proType = "web";
            NewProject newpro = new NewProject();
            newpro.Owner = this;
            if (newpro.ShowDialog() == true)
            {
                this.DialogResult = true;
                this.Close();
            }
            else { 
            }
        }

        private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        private void lbtn_close_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
            this.Close();
        }
    }
}
